// {+}

package mb.view;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;

public class AppMenu extends JMenuBar  implements ActionListener {

  private static final int EXIT	     = 1;
  private static final int SELECT    = 2;
  private static final int DESELECT	 = 3;
  private static final int HIDE		   = 4;
  private static final int SHOW		   = 5;

  private JMenu	menuFile               = new JMenu("File");
  private JMenuItem menuFileExit		   = new JMenuItem("Exit");
  private JMenu menuEdit 	             = new JMenu("Edit");
  private JMenuItem menuEditSelect 	   = new JMenuItem("Select Box");
  private JMenuItem menuEditDeselect   = new JMenuItem("Deselect Box");
  private JMenu menuView	             = new JMenu("View");
  private JMenuItem menuViewHide 		   = new JMenuItem("Hide Box");
  private JMenuItem menuViewShow 		   = new JMenuItem("Show Box");

  private AppFrame appFrame;

  public AppMenu() {
  	super();
    menuFileExit.setActionCommand("Exit");
    menuFileExit.addActionListener(this);

    menuEditSelect.setActionCommand("Select");
    menuEditSelect.addActionListener(this);
    menuEditDeselect.setActionCommand("Deselect");
    menuEditDeselect.addActionListener(this);

    menuViewHide.setActionCommand("Hide");
    menuViewHide.addActionListener(this);
    menuViewShow.setActionCommand("Show");
    menuViewShow.addActionListener(this);

    menuFile.add(menuFileExit);
    menuEdit.add(menuEditSelect);
    menuEdit.add(menuEditDeselect);
    menuView.add(menuViewHide);
    menuView.add(menuViewShow);

    this.add(menuFile);
    this.add(menuEdit);
    this.add(menuView);
	}

  public AppMenu(AppFrame aAppFrame) {
    this();
    this.setAppFrame(aAppFrame);
  }

  public AppFrame getAppFrame() {
  	return appFrame;
  }

  public void setAppFrame(AppFrame aFrame) {
  	appFrame = aFrame;
  }

  private int getMenuCommand(String aCommand) {
    int	action = -1;
    if      (aCommand.equals("Exit"))     action = EXIT;
    else if (aCommand.equals("Select"))   action = SELECT;
    else if (aCommand.equals("Deselect")) action = DESELECT;
    else if (aCommand.equals("Hide"))     action = HIDE;
    else if (aCommand.equals("Show"))     action = SHOW;
    return action;
  }

  public void actionPerformed(ActionEvent e) {
		String command = e.getActionCommand();
    switch (this.getMenuCommand(command)) {
    	case EXIT:      appFrame.exit();
                      break;
    	case SELECT:    appFrame.getDiagram().getBox().setSelected(true);
                      break;
    	case DESELECT:  appFrame.getDiagram().getBox().setSelected(false);
                      break;
    	case HIDE:      appFrame.getDiagram().getBox().setVisible(false);
                      break;
    	case SHOW:      appFrame.getDiagram().getBox().setVisible(true);
                      break;
    }
  }

}